package app16;

import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
    public static void main (String args[])
    {
        ServerSocket server=null;
        Server_thread thread;
        Socket you=null;
        while (true)
        {
            try {server=new ServerSocket(2022);}
            catch (IOException e1)
            {
                System.out.println(" 正在监听");//ServerSocket 对象不能重复创建
            }
            try {
                System.out.println("");
                System.out.println(" 正在等待客户呼叫……");
                you=server.accept();
                System.out.println(" 客户的地址："+you.getInetAddress());}
            catch (IOException e)
            {System.out.println(" 正在等待客户");}
            if (you!=null)
            {new Server_thread (you).start();}
        }
    }
}
class Server_thread extends Thread
{
    Socket socket;
    DataOutputStream out=null;
    DataInputStream in=null;
    String s=null;
    boolean quesion=false;
    Server_thread (Socket t)
    {
        socket=t;
        try{
            out=new DataOutputStream (socket.getOutputStream());
            in=new DataInputStream (socket.getInputStream());
        }
        catch (IOException e){}
    }
    public void run()
    {
        while (true)
        {
            double a[]=new double[2];
            int i=0;
            try {
                s=in.readUTF();
                quesion=false;
                System.out.println(s);
                StringTokenizer fenxi=new StringTokenizer(s,",");
                while (fenxi.hasMoreTokens())
                {
                    String temp=fenxi.nextToken();
                    try{a[i]=Double.valueOf(temp).doubleValue();
                        i++;}
                    catch (NumberFormatException e)
                    {
                        out.writeUTF("请输入数字字符");
                        quesion=true;
                    }
                }
                if (quesion==false)
                {
                    double p0=a[0]+a[1];
                    double p1=a[0]-a[1];
                    double p2=a[0]/a[1];//除数不能为0
                    double p3=a[0]*a[1];
                    out.writeUTF("和:"+ p0+"  差:"+p1+" 商:"+p2+"  积:"+p3);
                }
            }
            catch (IOException e)
            {
                System.out.println("客户离开");
                return;
            }
        }
    }
}

